<template>
  <div>
    <div>
      <span>编号 </span>
      <el-select v-model="id" placeholder="商铺完整编号">
        <el-option v-for="(item, index) in listId" :key="index" :label="item" :value="item">
        </el-option>
      </el-select>
      <span style="margin-left: 10px">客户姓名 </span>
      <el-input v-model="input" style="width: 200px" placeholder="请输入"></el-input>
      <span style="margin-left: 15px">状态 </span>
      <el-select v-model="statu" placeholder="请选择">
        <el-option v-for="statu in status" :key="statu.value" :label="statu.label" :value="statu.value">
        </el-option>
      </el-select>
      <span style="margin-left: 15px">收费项目 </span>
      <el-select v-model="chargeitem" placeholder="请选择">
        <el-option v-for="(item, index) in configFeeItem" :key="index" :label="item" :value="item">
        </el-option>
      </el-select>
      <br />
      <br />
      <span style="margin-top: 10px">付款方式 </span>
      <el-select v-model="paymethod" placeholder="请选择">
        <el-option v-for="(item, index) in paymethods" :key="index" :label="item.name" :value="item.code">
        </el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>

      <el-button type="primary" icon="el-icon-refresh" @click="reset">重置</el-button>
    </div>
    <div style="margin-top: 15px,text-align: left">
      <el-button type="primary" @click="visible = true" icon="el-icon-plus">新增</el-button>
      <el-dialog title="新建临时收费" :visible.sync="visible">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="客户类型">
            <!-- <el-radio-group > -->
            <el-radio v-model="radio" label="1">商铺客户</el-radio>
            <el-radio v-model="radio" label="2">非商铺客户</el-radio>
            <!-- </el-radio-group> -->
          </el-form-item>

          <el-form-item label="编号" prop="id" v-if="radio === '1'">
            <el-select v-model="ruleForm.id" placeholder="商铺完整编号">
              <el-option v-for="(item, index) in listId" :key="index" :label="item" :value="item">
              </el-option>
            </el-select>

            <el-button type="primary" icon="el-icon-search" @click="selectPaymentTemp">搜索</el-button>

          </el-form-item>
          <el-form-item label="客户姓名" v-if="radio == '1'" prop="name">
            <el-input v-model="ruleForm.name" :disabled="true"></el-input>
          </el-form-item>

          <el-form-item label="客户姓名" v-else prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>

          <el-form-item label="收费项目" prop="chargeitem">
            <el-select v-model="ruleForm.chargeitem" placeholder="请选择">
              <el-option v-for="(item, index) in configFeeItem" :key="index" :label="item" :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="付款方式" prop="paymethod">
            <el-select v-model="ruleForm.paymethod" placeholder="请选择">
              <el-option v-for="(item, index) in paymethods" :key="index" :label="item.name" :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="收费金额" prop="money">
            <el-input v-model="ruleForm.money" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="收费时间" required>
            <el-col :span="11">
              <el-form-item prop="date1">
                <el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>

          <el-form-item style="margin-left: 300px">
            <el-button type="primary" @click="resetForms('ruleForm')">取消</el-button>
            <el-button @click="submitForm">确定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

      <br />
      <br />
      <el-table :data="tableData" stripe style="width: 100%" :header-cell-style="{
          background: '#1890ff',
          color: '#ffffff',
          border: '1px #ffffff solid',
          'text-align': 'center',
        }" :cell-style="{ 'text-align': 'center' }">
        <el-table-column prop="id" label="序号"> </el-table-column>
        <el-table-column prop="resourceName" label="编号"> </el-table-column>
        <el-table-column prop="feeUser" label="客户姓名"> </el-table-column>
        <el-table-column prop="feeItemName" label="收费项目"></el-table-column>
        <el-table-column prop="payType" label="付款方式"></el-table-column>
        <el-table-column prop="amt" label="收费金额"> </el-table-column>
        <el-table-column prop="operateUser " label="收款人"> </el-table-column>
        <el-table-column prop="operateTime" label="收款时间"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.state === 'refunded'">未退款</el-tag>
            <el-tag type="danger" v-else>已退款</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="remark" label="备注"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button type="text" v-if="scope.row.state === 'refunded'" @click="refund(scope.row)">退款</el-button>
            <div style="text-align: left">
              <el-dialog title="新建" :visible.sync="visible1">
                <el-form :model="ruleForm1" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                  <el-form-item label="客户类型"> </el-form-item>
                  <el-form-item label="编号" prop="id" v-if="radio === '1'">
                    <el-select :disabled="true" v-model="ruleForm1.id" placeholder="商铺完整编号">
                      <el-option v-for="(item, index) in listId" :key="index" :label="item" :value="item" :disabled="true">
                      </el-option>
                    </el-select>
                  </el-form-item>

                  <el-form-item label="客户姓名" :disabled="true" prop="name">
                    <el-input v-model="ruleForm1.name" :disabled="true"></el-input>
                  </el-form-item>

                  <el-form-item label="收费项目" prop="chargeitem">
                    <el-select v-model="ruleForm1.chargeitem" :disabled="true" placeholder="请选择">
                      <el-option :disabled="true" v-for="chargeitem in chargeitems" :key="chargeitem.value" :label="chargeitem.label" :value="chargeitem.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="付款方式" prop="paymethod">
                    <el-select v-model="ruleForm1.paymethod" :disabled="true" placeholder="请选择">
                      <el-option :disabled="true" v-for="(item, index) in paymethods" :key="index" :label="item.name" :value="item.code">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="收费金额" prop="money" :disabled="true">
                    <el-input v-model="ruleForm1.money" :disabled="true" placeholder="请输入"></el-input>
                  </el-form-item>

                  <el-form-item label="收费时间" required :disabled="true">
                    <el-col :span="11">
                      <el-form-item prop="date1">
                        <el-date-picker :disabled="true" type="date" placeholder="选择日期" v-model="ruleForm1.date1" style="width: 100%"></el-date-picker>
                      </el-form-item>
                    </el-col>
                  </el-form-item>
                  <el-form-item label="退款时间" required>
                    <el-col :span="11">
                      <el-form-item prop="date2">
                        <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="ruleForm1.date2" style="width: 100%"></el-date-picker>
                      </el-form-item>
                    </el-col>
                  </el-form-item>

                  <el-form-item label="退款方式" prop="backmethod">
                    <el-select v-model="ruleForm1.backmethod" placeholder="请选择">
                      <el-option v-for="(item, index) in paymethods" :key="index" :label="item.name" :value="item.code">
                      </el-option>
                    </el-select>
                  </el-form-item>

                  <div style="display: flex">
                    <span>备注 </span>
                    <el-input style="width: 200px" type="textarea" :rows="2" placeholder="请输入内容" v-model="ruleForm1.remark">
                    </el-input>
                  </div>

                  <el-form-item style="margin-left: 300px">
                    <el-button type="primary" @click="resetForm('ruleForm')">取消</el-button>
                    <el-button @click="updateForm()">确定</el-button>
                  </el-form-item>
                </el-form>
              </el-dialog>
            </div>
            <el-button type="text" @click="shelf(scope.row)">打印</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="1" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "temporaryCharges",
  data() {
    return {
      id: "",
      listId: [],
      input: "",
      statu: "",
      status: [
        {
          value: "refunded",
          label: "未退款",
        },
        {
          value: "paied",
          label: "已退款",
        },
      ],
      configFeeItem: [],
      paymethod: "",
      paymethods: [],
      tableData: [],
      visible: false,
      visible1: false,
      radio: "1",

      ruleForm: {
        id: "",
        name: "",
        chargeitem: "",
        paymethod: "",
        money: "",
        date1: "",
      },

      ruleForm1: {
        uid: "",
        id: "",
        name: "",
        chargeitem: "",
        paymethod: "",
        money: "",
        date1: "",
        date2: "",
        backmethod: "",
        remark: "",
      },
      initialForm: {
        id: "",
        name: "",
        chargeitem: "",
        paymethod: "",
        money: "",
        date1: "",
      },

      rules: {
        id: [{ required: true, message: "未选择编号", trigger: "blur" }],

        name: [{ required: true, message: "未输入姓名", trigger: "blur" }],
        chargeitem: [
          { required: true, message: "未选择收费项目", trigger: "change" },
        ],
        paymethod: [
          { required: true, message: "未选择付款方式", trigger: "change" },
        ],
        money: [
          { required: true, message: "未选择收费金额", trigger: "change" },
        ],
        date1: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
      },

      currentPage: 1,
      pageSize: 5,
      total: 0,
    };
  },
  methods: {
    handleSizeChange(pageSize) {
      this.paymentTempList(this.currentPage, pageSize, "", "", "", "", "");
    },
    handleCurrentChange(pageNum) {
      this.paymentTempList(pageNum, this.pageSize, "", "", "", "", "");
    },
    // 请求表格数据信息
    paymentTempList(
      pageNum,
      pageSize,
      resourceName,
      feeUser,
      state,
      feeItemName,
      payType
    ) {
      this.$http
        .get("http://localhost:8080/selectTemporayChargesList", {
          params: {
            pageNum: pageNum,
            pageSize: pageSize,
            resourceName: resourceName,
            feeUser: feeUser,
            state: state,
            feeItemName: feeItemName,
            payType: payType,
          },
        })
        .then((result) => {
          let $data = result.data; //表格数据
          console.log($data);
          this.tableData = $data.data.list; //当前页
          this.currentPage = $data.data.pageNum; //每页显示条数
          this.pageSize = $data.data.pageSize; //每页显示条数
          this.total = $data.data.total; //总条数
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    payment_method() {
      this.$http
        .get("http://localhost:8080/payment_method", {})
        .then((result) => {
          let $data = result.data;
          let datalist = $data.data;
          this.paymethods = datalist;
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    payment_contract() {
      this.$http
        .get("http://localhost:8080/paymentContract", {})
        .then((result) => {
          let $data = result.data;
          let datalist = $data.data;
          this.listId = datalist;
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    // 临时收费
    selectPaymentTemp() {
      this.$http
        .get("http://localhost:8080/selectPaymentTemp", {
          params: {
            resourceName: this.ruleForm.id,
          },
        })
        .then((result) => {
          let $data = result.data;
          let datalist = $data.data;
          this.ruleForm.name = datalist[0].feeUser;
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    // 退款
    refund(row) {
      this.visible1 = true;
      this.ruleForm1.id = row.resourceName;
      this.ruleForm1.name = row.feeUser;
      this.ruleForm1.chargeitem = row.feeItemName;
      this.ruleForm1.paymethod = row.payType;
      this.ruleForm1.money = row.amt;
      this.ruleForm1.date1 = row.operateTime;
      this.ruleForm1.uid = row.id;
      this.ruleForm1.remark = row.remark;
    },
    // 收费项目
    configFeeItemName() {
      this.$http
        .get("http://localhost:8080/configFeeItemName", {})
        .then((result) => {
          let $data = result.data;
          let datalist = $data.data;
          this.configFeeItem = datalist;
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    // 添加
    submitForm() {
      this.$http
        .post("http://localhost:8080/addPaymentTempdata", this.ruleForm)
        .then((response) => {
          this.visible = false;
          this.paymentTempList();
        })
        .catch((error) => {
          console.log(error);
        });
    },

    // 搜索查询功能
    search() {
      this.paymentTempList(
        "1",
        "5",
        this.id,
        this.input,
        this.statu,
        this.chargeitem,
        this.paymethod
      );
    },

    // 重置
    reset() {
      this.id = "";
      this.input = "";
      this.statu = "";
      this.chargeitem = "";
      this.paymethod = "";
    },

    // 重置表单
    resetForms(form) {
      this.$refs[form].resetFields();
      this.ruleForm = Object.assign({}, this.initialForm);
      this.visible = false;
    },
    resetForm(form) {
      this.$refs[form].resetFields();
      this.ruleForm1 = Object.assign({}, this.initialForm);
      this.visible1 = false;
    },
    // 更新
    updateForm() {
      this.$http
        .post("http://localhost:8080/", {
          refundTime: this.ruleForm1.date2,
          remark: this.ruleForm1.remark,
          refundType: this.ruleForm1.backmethod,
          id: this.ruleForm1.uid,
        })
        .then((response) => {
          this.visible1 = false;
          // this.paymentTempList();
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
  mounted() {
    this.paymentTempList(this.currentPage, this.pageSize, "", "", "", "", "");
    this.payment_method();
    this.payment_contract();
    this.configFeeItemName();
  },
};
</script>

<style>
</style>